package ui 
{
	import event.CommEvent;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import log.Log;
	import net.message.IMsgBody;
	/**
	 * ...
	 * @author carlor
	 */
	public class DialogManager 
	{
		private var _uiLayer   : Sprite;
		private var _uiAction  : Object;
		private var _dialogList: Dictionary;
		
		public function DialogManager() 
		{
			_dialogList = new Dictionary();
			
		}
		
		public function get uiLayer(): Sprite
		{
			return _uiLayer;
		}
		
		public function set uiLayer(value: Sprite): void
		{
			_uiLayer = value;
			Engine.uiLayer.addChild(_uiLayer);
		}
		
		public function get uiAction(): Object
		{
			return _uiAction;
		}
		
		public function set uiAction(value: Object): void
		{
			_uiAction = value;
		}
		
		public function resize(): void
		{
			handlePacket("onResize");
		}
		
		public function findDialog(dialogClass: Class): Dialog
		{
			return _dialogList[dialogClass];
		}
		
		public function createDialog(dialogClass: Class): Dialog
		{
		
			var result: Dialog = findDialog(dialogClass);
			if (result) return result;
			
			result = new Dialog(new dialogClass());
			_dialogList[dialogClass] = result;
			_uiLayer.addChild(result.form);
			result.form.visible = false;
			
			return result;
		}
		
		public function changeParent(dialogClass: Class, parentClass: Class): void
		{
			var dialog: Dialog = findDialog(dialogClass);
			if (! dialog) return;
			var oldparent: Sprite; 

			var parentDialog: Dialog = findDialog(parentClass);
			if (! parentDialog){
				oldparent = dialog.form.parent as Sprite;
				if (oldparent) oldparent.removeChild(dialog.form);
				_uiLayer.addChild(dialog.form);
				dialog.form.dispatchEvent(new CommEvent(CommEvent.FORM_CHANGEOWNER).addData("old", oldparent).addData("new", _uiLayer));
			}else {
				oldparent = dialog.form.parent as Sprite;
				if (oldparent) oldparent.removeChild(dialog.form);
				parentDialog.form.addChild(dialog.form);
				dialog.form.dispatchEvent(new CommEvent(CommEvent.FORM_CHANGEOWNER).addData("old", oldparent).addData("new", parentDialog.form));
			}
		}
		
		public function getDialog(dialogClass: Class, beCreated: Boolean = true): Dialog
		{
			var result: Dialog = findDialog(dialogClass);
			if (! result && beCreated) {
				return createDialog(dialogClass);
			}else
				return result;
		}
		
		public function removeDialog(dialogClass: Class): void
		{
			var result: Dialog = findDialog(dialogClass);
			if (result) {
				if (result.form.parent) result.form.parent.removeChild(result.form);
				delete _dialogList[dialogClass];
				result.dispose();
				result = null;
			}
		}
		
		public function showDialog(dialogClass: Class, forceShow: Boolean=false): Dialog
		{
			var dialog: Dialog = getDialog(dialogClass, true);
			if (forceShow || ! dialog.form.visible)
				dialog.form.dispatchEvent(new CommEvent(CommEvent.DIALOG_SHOW));
			else
				dialog.form.dispatchEvent(new CommEvent(CommEvent.DIALOG_HIDE));
			return dialog;
		}
		
		public function hideDialog(dialogClass: Class, forceHide: Boolean = false): void
		{
			var dialog: Dialog = findDialog(dialogClass);
			if (! dialog) return;
			
			if (forceHide || dialog.form.visible) 
				dialog.form.dispatchEvent(new CommEvent(CommEvent.DIALOG_HIDE));
			else
				dialog.form.dispatchEvent(new CommEvent(CommEvent.DIALOG_SHOW));
		}
		
		public function hideDialogs(): void
		{
			for each (var dialog: Dialog in _dialogList) {
				if (dialog.form.visible)
					dialog.form.dispatchEvent(new CommEvent(CommEvent.DIALOG_HIDE));
			}
		}
		
		public function handlePacket(op: String, msg: * = null): void
		{
			if (_uiAction.hasOwnProperty(op))
				_uiAction[op](msg);
			for each (var dialog: Dialog in _dialogList) {
				try {
					if (dialog.form.hasOwnProperty(op))
						dialog.form[op](msg);
				}
				catch (err: Error) {
					Log.errorLog(err.message);
				}
			}
		}
	}

}